package com.example.homeworkdev.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.homeworkdev.entity.User;

public class UserInfoShared {

    // 保存用户信息到SharedPreferences
    public void saveUserInfo(Context context, String phone, String password,boolean rememberMe) {
        SharedPreferences preferences = context.getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("phone", phone);
        editor.putString("password", password);
        editor.putBoolean("rememberMe",rememberMe);
        editor.putBoolean("isLoggedIn", true);
        editor.apply();
    }

    // 从SharedPreferences获取用户信息
    public User getUserInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        String phone = preferences.getString("phone", "");
        String password = preferences.getString("password", "");
        boolean rememberMe = preferences.getBoolean("rememberMe",false);
        boolean isLoggedIn = preferences.getBoolean("isLoggedIn", false);

        if (isLoggedIn) {
            return new User(phone,password);
        }
        return null;
    }

    public void clearUserInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();
    }

}
